package com.jy.adscm.operation.hscrb.controller;


import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.hscrb.bean.LjzzPartsmade;
import com.jy.adscm.operation.hscrb.service.LjzzPartsmadeService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-11-23
 */
@RestController
@RequestMapping("/adscm/operation/ljzzPartsmade")
public class LjzzPartsmadeController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    LjzzPartsmadeService LjzzPartsmadeService;
    @RequestMapping("insert")
   public SimpleCoreResponse insert(@RequestBody LjzzPartsmade ljzzPartsmade, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        ljzzPartsmade.setTaskcode(  Long.valueOf(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())) );
        ljzzPartsmade.setTaskreleaseperson(token.getUser().getId().toString());
        ljzzPartsmade.setTaskreleaseaddtime(new Date());
        ljzzPartsmade.setTaskfinishstate(0);
        ljzzPartsmade.setTaskstate(0);
        ljzzPartsmade.setIstaskstart("0");
        ljzzPartsmade.setIstaskbegin("0");
        ljzzPartsmade.setIstaskend("0");
        ljzzPartsmade.setIsreviewtask("0");
        ljzzPartsmade.setReviewresult("-1");
        boolean insert = LjzzPartsmadeService.insert(ljzzPartsmade);
        return new SimpleCoreResponse("200","新增成功",insert);
   }

    @RequestMapping("delete/{id}")
   public SimpleCoreResponse delete(@PathVariable Integer id)throws AdscmSystemException {
        boolean result = LjzzPartsmadeService.deleteById(id);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody LjzzPartsmade ljzzPartsmade, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = LjzzPartsmadeService.updateLjzzPartsmade(ljzzPartsmade,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectLjzzPartsmade/{taskState}/{taskFinishState}/{isReviewTask}")
    public  SimpleCoreResponse  selectLjzzPartsmade(@PathVariable Integer taskState ,@PathVariable Integer taskFinishState,@PathVariable Integer isReviewTask) throws  AdscmSystemException{
        List<LjzzPartsmade> ljzzPartsmades = LjzzPartsmadeService.selectLjzzPartsmade(taskState,taskFinishState,isReviewTask);
        return  new SimpleCoreResponse("200","查询成功",ljzzPartsmades);
    }

    @RequestMapping("selectTaskPerformPerson")
    public  SimpleCoreResponse  selectTaskPerformPerson() throws  AdscmSystemException{
        List<JyUser> users = LjzzPartsmadeService.selectTaskPerformPerson();
        return  new SimpleCoreResponse("200","查询成功",users);
    }
}

